package org.example.lethear.config;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * Swagger API文档配置
 */
@Configuration
public class SwaggerConfig {

    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .openapi("3.0.1")
                .info(apiInfo())
                .servers(List.of(
                        new Server().url("http://localhost:8080").description("开发环境"),
                        new Server().url("https://api.yourdomain.com").description("生产环境")
                ))
                .components(new Components()
                        .addSecuritySchemes("bearerAuth", 
                                new SecurityScheme()
                                        .type(SecurityScheme.Type.HTTP)
                                        .scheme("bearer")
                                        .bearerFormat("JWT")
                                        .description("JWT认证令牌")
                        )
                )
                .addSecurityItem(new SecurityRequirement().addList("bearerAuth"));
    }

    private Info apiInfo() {
        return new Info()
                .title("LetHear API 文档")
                .description("LetHear - 一个基于Spring Boot和Vue的内容管理系统\n\n" +
                        "## 功能特性\n" +
                        "- 用户认证和授权\n" +
                        "- 帖子发布和管理\n" +
                        "- 评论和互动\n" +
                        "- 悄悄话功能\n" +
                        "- 文件上传和管理\n" +
                        "- 管理员后台\n\n" +
                        "## 认证说明\n" +
                        "大部分API需要JWT令牌认证，请先通过登录接口获取token，然后在请求头中添加：\n" +
                        "```\n" +
                        "Authorization: Bearer <your-jwt-token>\n" +
                        "```\n\n" +
                        "## 响应格式\n" +
                        "所有API响应都使用统一的格式：\n" +
                        "```json\n" +
                        "{\n" +
                        "  \"code\": 200,\n" +
                        "  \"message\": \"操作成功\",\n" +
                        "  \"data\": {},\n" +
                        "  \"timestamp\": \"2024-01-01T00:00:00\"\n" +
                        "}\n" +
                        "```\n\n" +
                        "## 状态码说明\n" +
                        "- 200: 成功\n" +
                        "- 400: 请求参数错误\n" +
                        "- 401: 未认证\n" +
                        "- 403: 无权限\n" +
                        "- 404: 资源不存在\n" +
                        "- 500: 服务器内部错误")
                .version("1.0.0")
                .contact(new Contact()
                        .name("LetHear Team")
                        .email("support@lethear.com")
                        .url("https://github.com/lethear/lethear"))
                .license(new License()
                        .name("MIT License")
                        .url("https://opensource.org/licenses/MIT"));
    }
} 